<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2019/1/22
 * Time: 8:47 PM
 */
namespace App\Controllers;
use App\Models\Entity\User;
use SebastianBergmann\CodeCoverage\Report\Xml\Report;
use Swoft\Http\Message\Server\Request;

use Swoft\Bean\Annotation\Inject;
use Swoft\Db\Db;
use Swoft\Http\Message\Bean\Annotation\Middleware;
use Swoft\Http\Message\Stream\SwooleStream;
use Swoft\Http\Server\Bean\Annotation\Controller;
use Swoft\Http\Server\Bean\Annotation\RequestMapping;
use App\Test\TokenMiddWare;
use Swoole\Http\Response;

/**
 * Class TestController
 * @Controller()
 */
class TestController
{
    /**
     * @Inject()
     * @var \Swoft\Redis\Redis
     */
    private $redis;

    /**
     * @RequestMapping()
     */
    public function index(){
        $getUser = Db::query('select * from user where id=1')->getResult();
        return $getUser;
//        return $this->redis->get("name");
    }
    /**
     * @RequestMapping()
     */
    public function test(Request $request){
        return $request->getHeader("token_user_name");
//        获取请求所有对象
        return $request->getHeaders();
    }
    /**
     * 文件下载
     * @RequestMapping()
     */
    public function xia(\Swoft\Http\Message\Server\Response $response){
        $fileNam=BASE_PATH."/abc.text";
        $file=fopen($fileNam,"rb");
        return $response->withHeader("Content-type","application/octet-stream")
            ->withHeader('Content-Disposition','attachment; filename=abc.txt')
            ->withBody(new SwooleStream( fread($file,filesize($fileNam))));
    }

}
